/**
 * Created by chenyong on 2015/2/5.
 */

var Hashids = require("hashids");

module.exports = Utils;

function Utils() {

};

Utils.prototype = {
    //根据当前时间生成一个随机字符串
    randomstr:function(len){
        var now = new Date();
        var _hashids = new Hashids('bigpixel',len,"abcdefghijklmnopqrstuvwxyz0123456789");
        return _hashids.encode(now.getTime());
    },
    //从Request请求中获取客户端IP
    getClientIp:function(req){
        var ipAddress;
        var forwardedIpsStr = req.header('x-forwarded-for');
        if (forwardedIpsStr) {
            var forwardedIps = forwardedIpsStr.split(',');
            ipAddress = forwardedIps[0];
        }
        if (!ipAddress) {
            ipAddress = req.connection.remoteAddress;
        }
        return ipAddress;
    },
    //年月字符串
    getCurrentYearMonthString:function(sep){
        var now = new Date();
        var year = now.getFullYear().toString();
        var month = now.getMonth().toString();
        if(month.length <2)
        {
            month = "0" + month;
        }
        return year + sep + month;
    }
}
